package org.shilong.Utility.Loader;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.shilong.Loader.ImageLoader;
import org.shilong.MContext.Json.JSonContext;
import org.shilong.MContext.WebRequest.WebRequestContext;
import org.shilong.Model.FriendModel;
import org.shilong.UI.R;
import org.shilong.UI.friend.ShowFriendDetailFrame;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.ToastUtility;
import org.shilong.Utility.constants.APIConstants;
import org.shilong.Utility.constants.WeiboConstants;

import com.weibo.net.Oauth2AccessTokenHeader;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewFriendDetailListViewLoader {


	private ListView mListView;


	private Activity mContext;
	private String wei_id;

	public NewFriendDetailListViewLoader(ListView lv, Activity con, String wei_id) {

		this.wei_id = wei_id;

		this.mListView = lv;

		this.mContext = con;

	}

	public void startLoader() {

		mHandler.post(new Job(wei_id));
	}

	private Handler mHandler = new Handler();

	private class Job implements Runnable {

		private String weiboid;

		public Job(String id) {

			this.weiboid = id;

		}

		@Override
		public void run() {
			// TODO Auto-generated method stub

			System.out.println("他/她的微博id是:" + weiboid);
			ToastUtility.showToast( mContext, "开始获取他/T她的微博好友..");

			WeiboParameters params =  new WeiboParameters();

			params.add("uid", weiboid );	// 好像是好用

			Utility.setAuthorization( new  Oauth2AccessTokenHeader() );

			List<FriendModel> list = new ArrayList<FriendModel>();

			try {
				String friendsRlt = Weibo.getInstance().request(mContext, APIConstants.GET_FRIEND,
						params, Utility.HTTPMETHOD_GET,
						Weibo.getInstance().getAccessToken());
				
				list = getFriendList( friendsRlt );
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			mListView.setAdapter( new MyAdapter(list));

		}

	}

	/**
	 * 获取好友id的列表
	 * @param rlt
	 * @return
	 */
	private List<FriendModel> getFriendList( String rlt ){

		List<FriendModel> friList = new ArrayList<FriendModel>();
		
		try {
			JSONObject obj = new JSONObject(rlt);
			
			String IDARR = obj.getString("ids");
			

			IDARR  = IDARR.replace("[", "");
			IDARR  = IDARR.replace("]", "");

			String[] arr = IDARR.split(",");

			for (String string : arr) {

				Weibo mWeibo = Weibo.getInstance();

				WeiboParameters params = new WeiboParameters();
				params.add( "uid", string );

				Utility.setAuthorization( new  Oauth2AccessTokenHeader() );
				
				String re = mWeibo.request( mContext , "https://api.weibo.com/2/users/show.json", params, Utility.HTTPMETHOD_GET, mWeibo.getAccessToken());

				JSONObject oo = new JSONObject( re );

				FriendModel model = new FriendModel();

				model.setWeibo_id( oo.getString("id"));
				model.setScreen_name( oo.getString("screen_name"));
				model.setProfile_image_url( oo.getString("profile_image_url") + ".jpg");
				model.setLocation( oo.getString(  "location"  ) );
				model.setDescription( oo.getString( "description" ));
				
				friList.add(model);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
		return friList;

	}
	

	private class MyAdapter extends BaseAdapter {

		private List<FriendModel> mList;

		public MyAdapter(List<FriendModel> mList) {

			this.mList = mList;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub

			final FriendModel model = mList.get(arg0);


			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View root = inflater.inflate(
					R.layout.inner_friend_listview_item_layout, null);

			/**
			 * 初始化头像
			 */
			final ImageView iv = (ImageView) root
					.findViewById(R.id.inner_show_header_img_imageview);

			ImageLoader loader = new ImageLoader();

			Drawable draw = loader.loadImage(model.getProfile_image_url(), new ImageLoader.ImageCallBack() {

				@Override
				public void setImageDrawable(Drawable imageDrawable) {
					// TODO Auto-generated method stub

					iv.setImageDrawable(imageDrawable);
				}
			});

			if (draw != null)
				iv.setImageDrawable(draw);

			TextView inner_friend_screen_name_textview = (TextView) root
					.findViewById(R.id.inner_friend_screen_name_textview);
			TextView inner_friend_location_textview = (TextView) root
					.findViewById(R.id.inner_friend_location_textview);
			TextView inner_friend_desc_textview = (TextView) root
					.findViewById(R.id.inner_friend_desc_textview);

			inner_friend_screen_name_textview.setText(model.getScreen_name());
			inner_friend_location_textview.setText(model.getLocation());
			inner_friend_desc_textview.setText(model.getDescription());


			return root;

		}
	}

}
